package org.example.multiThreading.threadPool;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestCachedThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*
        CachedThreadPool
        缓存线程池。没有核心线程，普通线程数量为Integer.MAX_VALUE（可以理解为无限），
        线程闲置60s后回收，任务队列使用SynchronousQueue这种无容量的同步队列。适用于任务量大但耗时低的场景。
        */
        ExecutorService service = Executors.newCachedThreadPool();

        //创建TestCallable，并加入线程池
        for (int i = 0; i < 10; i++) {
            TestCallable callable = new TestCallable();
            System.out.println(service.submit(callable).get());
        }

        //关闭线程池
        service.shutdown();
    }
}
